package io.renren.modules.jxgk.controller;

import io.renren.common.echarts.Series;
import io.renren.common.utils.R;
import io.renren.modules.jxgk.entity.TempStudentAttendenceEntity;
import io.renren.modules.jxgk.service.StudentChartsService;
import io.renren.modules.jxgkphone.entity.CourseDate;
import io.renren.modules.jxgkphone.service.CourseDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/StudentCharts")
public class StudentChartsController {
    @Autowired
    CourseDateService courseDateService;
    @Autowired
    StudentChartsService studentChartsService;
    @RequestMapping("/studentAttendence")
    public R getstudentAttendence(@RequestParam Map<String, Object> params) {
        String option=((String)params.get("option"));
        String datas=((String)params.get("datas"));
        String data[]=datas.split(",");
    //    String college[]=colleges.split(",");
        List<String> sevenDate=new ArrayList<String>();
        HashMap<String,Series> hm=new HashMap<String,Series>();
        HashMap<String,Series> hm1=new HashMap<String,Series>();
        for(int i=6;i>=0;i--){
            sevenDate.add(getStatetime(i));
        }
        for(int i=0;i<data.length;i++){
            Series series=new Series();
            series.setName(data[i]);
            series.setType("bar");
            List<Integer> list=new ArrayList<Integer>();
            for(int j=0;j<7;j++) {
                list.add(null);
            }
            series.setData(list);
            hm.put(data[i],series);
        }
        for(int i=0;i<data.length;i++){
            Series series=new Series();
            series.setName(data[i]);
            series.setType("bar");
            List<Integer> list=new ArrayList<Integer>();
            for(int j=0;j<7;j++) {
                list.add(null);
            }
            series.setData(list);
            hm1.put(data[i],series);
        }
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("groupby",option);
        map.put("datelist",sevenDate);
        map.put("data",data);
        List<TempStudentAttendenceEntity> list=studentChartsService.getAbsenceNumByDate(map);
        List<TempStudentAttendenceEntity> Alllist=studentChartsService.getAllNumByDate(map);
        for(TempStudentAttendenceEntity tsa:list){
            Series cur=null;
            if(option.equals("college")) {
                cur = hm.get(tsa.getDepartment());
            }else if(option.equals("class")){
                cur = hm.get(tsa.getClassname());
            }else {
                cur=hm.get(option);
            }
            List curlist=cur.getData();
            curlist.set(sevenDate.indexOf(tsa.getDate()),tsa.getCount());
        }
        for(TempStudentAttendenceEntity tsa:Alllist){
            Series cur=null;
            String opt="";
            if(option.equals("college")) {
                opt=tsa.getDepartment();
            }else if(option.equals("class")){
                opt=tsa.getClassname();
            }
            cur=hm1.get(opt);
            List curlist=cur.getData();
            int index=sevenDate.indexOf(tsa.getDate());
            Integer absence=(Integer)hm.get(opt).getData().get(index);
            if(absence!=null){
                double total=(double)tsa.getCount();
                String res=String.format("%.2f", (total-absence)*100/total);
                curlist.set(index,res);
            }
            else {
                curlist.set(index, 100);
            };
        }
        return R.ok().put("map",hm).put("datelist",sevenDate).put("map1",hm1);
    }
    //获得前几天的日期
    public  String getStatetime(int i) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, - i);
        Date monday = c.getTime();
        String preMonday = sdf.format(monday);
        return preMonday;
    }
    public static  void main(String args[]){
      //  System.out.println(getStatetime(0));
        String s="$14";
        s=s.replace("$","");
        String ss[]=s.split("-");
        for(int i=0;i<ss.length;i++){
            System.out.println(ss[i]);
        }
    }
}
